package android.explorer;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class PictureActivity extends Activity implements OnClickListener{
	private ImageView image;
	 @Override
	 public void onCreate(Bundle savedInstanceState){
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.picturespace);
		 Button chose = (Button)findViewById(R.id.chose);
		 chose.setOnClickListener(this);
		 Button btn = (Button) findViewById(R.id.back);
		 btn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PictureActivity.this, Explorer.class);
				startActivity(intent);
			}
		});
		btn.setLongClickable(true);
		btn.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				Intent choseMusic = new Intent(Intent.ACTION_VIEW);
				File sdcard = Environment.getExternalStorageDirectory();
				File music = new File(sdcard.getPath()+"/download/phyane.mp3");
				choseMusic.setDataAndType(Uri.fromFile(music), "audio/mp3");
				startActivity(choseMusic);
				return true;
			}
		});
		image = (ImageView)findViewById(R.id.photoShow);
		 
		 
	 }
	 
	 
	 protected void onActivityResult(int requestCode, int resultCode, Intent intent){
		 super.onActivityResult(requestCode, resultCode, intent);
		 if(resultCode == RESULT_OK){
			 Uri imgUri = intent.getData();
			 Display display = getWindowManager().getDefaultDisplay();
			 int width = display.getWidth();
			 int height = display.getHeight();
			 
			 Options bmpOps = new Options();
			 bmpOps.inJustDecodeBounds = true;
			 try {
				Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri),
						null,bmpOps);
				int wr = (int)Math.ceil(bmpOps.outWidth/(float)width);
				int hr = (int)Math.ceil(bmpOps.outHeight/(float)height);
				if(hr > 1 && wr > 1){
					if(hr > wr){
						bmpOps.inSampleSize = hr;
					}else{
						bmpOps.inSampleSize = wr;
					}
				}
				bmpOps.inJustDecodeBounds = false;
				bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri), 
						null, bmpOps);
				image.setImageBitmap(bmp);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				Log.v("Error", e.toString());
			}
		 }
	 }


	@Override
	public void onClick(View v) {
		Intent phyane = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(phyane, 0);
		System.out.println("Clicked");
	}
	 
}
